//
//  HRZStoreViewController.m
//  EMarket
//
//  Created by 物联速通信息技术有限公司 on 2018/5/22.
//  Copyright © 2018 好日子. All rights reserved.
//

#import "HRZStoreViewController.h"


// Controllers
#import "HRZGoodsDetailViewController.h"
// Models
#import "HRZStoreModel.h"
#import "HRZGoodsTypeModel.h"
#import "HRZGoodsModel.h"
// Views
#import "HRZStoreMainHeadCell.h"
#import "HRZStoreMainRecommendGoodsCell.h"
#import "HRZGoodsGridTableCell.h"
#import "HRZGoodsTypesView.h"
// Vendors
#import <MJExtension.h>

//#import "HRZShowGoodsSimpleInfoCell.h"
//#import "HRZ_BrandsSortHeaderView.h"

@interface HRZStoreViewController ()< UITableViewDelegate, UITableViewDataSource, HRZGoodsTypesViewDelegate>

//@property (nonatomic, strong) UICollectionView *collectionView;

@property (nonatomic, strong) UITableView *mTableView;

@end


//static NSString * const HRZBrandsSortHeadViewID             = @"HRZ_BrandsSortHeaderView";
//static NSString * const HRZShowGoodsSimpleInfoCellID        = @"HRZShowGoodsSimpleInfoCell";

static NSString * const HRZStoreMainHeadCellID              = @"HRZStoreMainHeadCell";
static NSString * const HRZStoreMainRecommendGoodsCellID    = @"HRZStoreMainRecommendGoodsCell";
static NSString * const HRZGoodsGridTableCellID             = @"HRZGoodsGridTableCell";
static NSString * const HRZGoodsTypesViewID                 = @"HRZGoodsTypesView";


@implementation HRZStoreViewController
- (UITableView *)mTableView{
    if (!_mTableView) {
        _mTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _mTableView.delegate = self;
        _mTableView.dataSource = self;
        [self.view addSubview:_mTableView];
        
        _mTableView.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight - HRZTopNavH);
        _mTableView.estimatedRowHeight = 90;
        _mTableView.rowHeight = UITableViewAutomaticDimension;
        [_mTableView setTableFooterView:[UIView new]];
        _mTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        
        /** 注册cell */
        [_mTableView registerNib:[UINib nibWithNibName:NSStringFromClass([HRZStoreMainHeadCell class]) bundle:nil] forCellReuseIdentifier:HRZStoreMainHeadCellID];
        [_mTableView registerNib:[UINib nibWithNibName:NSStringFromClass([HRZStoreMainRecommendGoodsCell class]) bundle:nil] forCellReuseIdentifier:HRZStoreMainRecommendGoodsCellID];
        [_mTableView registerNib:[UINib nibWithNibName:NSStringFromClass([HRZGoodsGridTableCell class]) bundle:nil] forCellReuseIdentifier:HRZGoodsGridTableCellID];
        
    }
    return _mTableView;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self setUpdateUI];
    
    [self setUpData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)setUpdateUI {
    self.mTableView.backgroundColor = HRZBGColor;
}

- (void)setUpData {
    NSArray *ar = [HRZStoreModel mj_objectArrayWithFilename:@"Store001.plist"];
    _storeModel =ar[0];
    self.navigationItem.title = _storeModel.storeName;
    [_mTableView reloadData];
}

- (void)setStoreModel:(HRZStoreModel *)storeModel {
    _storeModel = storeModel;
    
}

- (void)setSupplierItem:(HRZSupplierItem *)supplierItem {
    _supplierItem = supplierItem;
//    self.title = supplierItem.supplierName;
//    [_collectionView reloadData];
}

#pragma mark - ===============  UITableViewDataSource  ===============
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    if (_storeModel) return 2;
    return 0;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) return 3;
    if (section == 1) return 1;
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell;
    
    /** 门店 title 推荐 新品 */
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            HRZStoreMainHeadCell *headCell = [tableView dequeueReusableCellWithIdentifier:HRZStoreMainHeadCellID];
            headCell.storeModel = _storeModel;
            headCell.FollowBlock = ^(BOOL isFollowing) {
                _storeModel.isUserFollow = isFollowing;
                [tableView reloadData];
            };
            cell = headCell;
        }
        if (indexPath.row == 1) {
            HRZStoreMainRecommendGoodsCell *recmCell = [tableView dequeueReusableCellWithIdentifier:HRZStoreMainRecommendGoodsCellID];
            recmCell.goodsModel = _storeModel.goodsTypeArray[0].goodsArray[0];
            cell = recmCell;
        }
        if (indexPath.row == 2) {
            HRZGoodsGridTableCell *goodsGridCell  = [tableView dequeueReusableCellWithIdentifier:HRZGoodsGridTableCellID];
            goodsGridCell.goodsTypeArray = [NSMutableArray arrayWithObject:[_storeModel.goodsTypeArray lastObject]];
            [goodsGridCell updateConstraints];
            cell = goodsGridCell;
        }
    }
    
    /** 门店商品信息 */
    if (indexPath.section == 1) {
        
        HRZGoodsGridTableCell *goodsGridCell  = [tableView dequeueReusableCellWithIdentifier:HRZGoodsGridTableCellID];
        goodsGridCell.goodsTypeArray = _storeModel.goodsTypeArray;
        
        [goodsGridCell updateConstraints];
        
        cell = goodsGridCell;
    }
    
    return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (section == 1) {
        HRZGoodsTypesView *goodsTypeView = [[HRZGoodsTypesView alloc] init];
        goodsTypeView.goodsTypeArray = _storeModel.goodsTypeArray;
        goodsTypeView.delegate = self;
        goodsTypeView.backgroundColor = HRZBGColor;
        return goodsTypeView;
    }
    
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 1) return 50;
    return 0;
}

#pragma mark - ===============  HRZGoodsTypesViewDelegate  ===============
- (void)onGoodsTypesViewDidSelect:(NSIndexPath *)indexpath {
    NSLog(@"%ld",(long)indexpath.row);
}


@end
